package com.web.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.annotation.CurrentUser;
import com.web.annotation.LoginRequired;
import com.web.controller.pay.AliPayDto;
import com.web.domain.Response;
import com.web.entity.*;
import com.web.service.LoginService;
import com.web.service.VipService;
import com.web.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *会员
 */
@Slf4j
@RestController
@RequestMapping(value = "/app")
public class VipController extends BaseController{
    @Autowired
    private VipService vipService;
    @Autowired
    private LoginService loginService;


    /**
     * 获取所有会员
     * @return
     */
    @PostMapping("/vip/getVipInfo")
    @LoginRequired
    public Response getVIPInfo(){
        List<VipInfo> list = vipService.getVIPInfo();

        return Response.custom().ok().addParams(list);
    }

    /**
     *开通会员
     * @param info
     * @param vipId
     * @param payType 支付类型1.支付宝2.微信
     * @return
     */
    @RequestMapping("/create/vip")
    public Response createVIP(@CurrentUser UserInfo info,Integer vipId,Integer payType){
        if (vipId == null || payType == null){
            return Response.custom().failure("请求参数异常!");
        }
        VipInfo vipInfo = vipService.selectById("vip_basis_id",vipId.toString());
        if (vipInfo == null){
            return Response.custom().failure("未找到该VIP类型!");
        }
        if (payType != 1 && payType != 2){
            return Response.custom().failure("支付类型不正确!");
        }
        RMBRecord rmbRecord = new RMBRecord(UUIDUtil.getByUUId(),payType,vipId.toString(),
                                      "开通会员-"+vipInfo.getVip_basis_id(),vipInfo.getPrice(),2,info.getUserId(),
                                    null,new Date(),new Date());
        vipService.addRMBRecord(rmbRecord);

        return getResponse(payType, rmbRecord);
    }

    /**
     * 获取模豆和VIP基本信息
     * @return
     */
    @RequestMapping("/chargeManage/Info")
    @LoginRequired
    public Response chargeManageInfos(){
        List<ChargeManageInfo> chargeManageInfo = vipService.chargeManageInfos();
        List<VipInfo> list = vipService.getVIPInfo();
        Map<String,Object> map = new HashedMap();
        map.put("charge",chargeManageInfo);
        map.put("vipInfo",list);
        return Response.custom().ok().addParams(map);
    }

    /**
     * 充值模豆
     * @param info
     * @param chargeManageId
     * @param payType
     * @return
     */
    @RequestMapping("/chargeManage")
    public Response chargeManage(@CurrentUser UserInfo info,Integer chargeManageId,Integer payType){
        if (chargeManageId == null){
            return Response.custom().failure("缺少字段!");
        }
        ChargeManageInfo chargeManageInfo = vipService.selectByIdChargeManageInfo("id",chargeManageId.toString());
        if (chargeManageId == null){
            return Response.custom().failure("传入ID异常!");
        }
        if (payType != 1 && payType != 2){
            return Response.custom().failure("支付类型不正确!");
        }
        RMBRecord rmbRecord = new RMBRecord(UUIDUtil.getByUUId(),payType,chargeManageInfo.getId().toString(),
                "模豆充值-"+chargeManageId,chargeManageInfo.getAccount(),2,info.getUserId(),
                null,new Date(),new Date());
        vipService.addRMBRecord(rmbRecord);
        return getResponse(payType, rmbRecord);
    }


    /**
     * 付费模豆
     * @param info
     * @param typeId
     * @param type 1.查阅资料2.观看视频3.联系某人
     * @return
     */
    @RequestMapping("/chargeManage/pay")
    public Response chargeManagePay(@CurrentUser UserInfo info,String typeId,Integer type){
        if (StringUtils.isBlank(typeId) || type == null){
            return Response.custom().failure("缺少必要字段!");
        }
        if (0 > type || type > 3){
            return Response.custom().failure("付费类型不正确!");
        }
        UserInfo userInfo = findByUser("user_id",info.getUserId());
        if (userInfo == null){
            return Response.custom().failure("对不起,没有找到你这个用户!");
        }
        switch (type){
            case 1:
                UserData data = vipService.selectByIdUserData("id", typeId);
                if (data == null){
                    return Response.custom().failure("ID错误!");
                }
                if (data.getPrice() <= 0){
                    return Response.custom().failure("该资料为免费视频无须付费!");
                }
                if (userInfo.getCurrency() < data.getPrice()){
                    return Response.custom().failure("你的模豆余额不足，请进行充值!");
                }
                //插入模豆流水记录
                loginService.addCurrencyRecord(- data.getPrice().intValue(),info.getCurrency(),info.getUserId(),data.getUser_id(),4,info.getIsVip());
                //减少模豆
                info.setCurrency(info.getCurrency() - data.getPrice().intValue());
                loginService.uploadUserInfo(info);

                UserInfo toUserInfo = findByUser("user_id",data.getUser_id());
                //增加模豆
                toUserInfo.setCurrency(toUserInfo.getCurrency() + data.getPrice().intValue());
                loginService.uploadUserInfo(toUserInfo);

                return Response.custom().ok("支付成功!");
            case 2:
                UserData data2 = vipService.selectByIdUserData("id", typeId);
                if (data2 == null){
                    return Response.custom().failure("ID错误!");
                }
                if (data2.getPrice() <= 0){
                    return Response.custom().failure("该视频为免费视频无须付费!");
                }
                if (userInfo.getCurrency() < data2.getPrice()){
                    return Response.custom().failure("你的模豆余额不足，请进行充值!");
                }
                //插入模豆流水记录
                loginService.addCurrencyRecord(-data2.getPrice().intValue(),info.getCurrency(),info.getUserId(),data2.getUser_id(),5,info.getIsVip());
                //减少模豆
                info.setCurrency(info.getCurrency() - data2.getPrice().intValue());
                loginService.uploadUserInfo(info);

                UserInfo toUserInfo2 = findByUser("user_id",data2.getUser_id());
                //增加模豆
                toUserInfo2.setCurrency(toUserInfo2.getCurrency() + data2.getPrice().intValue());
                loginService.uploadUserInfo(toUserInfo2);
                return Response.custom().ok("支付成功!");
            case 3:
                Double amount = getMarginInfo().getContactCost();
                if (userInfo.getCurrency() < amount){
                    return Response.custom().failure("你的模豆余额不足，请进行充值!");
                }
                //插入模豆流水记录
                loginService.addCurrencyRecord(-amount.intValue(),info.getCurrency(),info.getUserId(), typeId,6,info.getIsVip());
                //减少模豆
                info.setCurrency(info.getCurrency() - amount.intValue());
                loginService.uploadUserInfo(info);

                UserInfo toUserInfo3 = findByUser("user_id", typeId);
                //增加模豆
                toUserInfo3.setCurrency(toUserInfo3.getCurrency() + amount.intValue());
                loginService.uploadUserInfo(toUserInfo3);

                return Response.custom().ok("支付成功!");
            default:
                return Response.custom().failure("付费类型不正确");
        }
    }

    /**
     * 获取我的模豆使用明细
     * @param info
     * @param pageNum
     * @return
     */
    @RequestMapping("/md/details")
    public Response mdDetails(@CurrentUser UserInfo info,Integer pageNum){
        PageHelper.startPage(pageNum,15);
        List<CurrencyDetailsBean> list = vipService.getCurrencyDetails(info.getUserId());
        PageInfo pageInfo = new PageInfo(list,15);
        if(pageNum > pageInfo.getPages()){
            return Response.custom().ok().addParams(new List[0]);
        }else{
            return Response.custom().ok().addParams(pageInfo.getList());
        }
    }

    /**
     * 获取我的模豆使用明细
     * @param info
     * @param pageNum
     * @return
     */
    @RequestMapping("/md/bzj/details")
    public Response mdBZJDetails(@CurrentUser UserInfo info,Integer pageNum){
        PageHelper.startPage(pageNum,15);
        List<CurrencyDetailsBZJBean> list = vipService.getCurrencyDetailsBZJ(info.getUserId());
        PageInfo pageInfo = new PageInfo(list,15);
        if(pageNum > pageInfo.getPages()){
            return Response.custom().ok().addParams(new List[0]);
        }else{
            return Response.custom().ok().addParams(pageInfo.getList());
        }
    }


    private Response getResponse(Integer payType, RMBRecord rmbRecord) {
        switch (payType){
            case 1:
                return Response.custom().ok().addParams(AliPay(rmbRecord.getRmbId(),rmbRecord.getType(),rmbRecord.getMoney()));
            case 2:
                return Response.custom().ok().addParams(WeChatPay(rmbRecord.getRmbId(),rmbRecord.getType(),rmbRecord.getMoney()));
            default:
                return Response.custom().failure("支付类型不正确!");
        }
    }
}
